﻿using System;
using CoreLibrary;
using CoreLibrary.Date;
using CoreLibrary.Template;
using Entities.DataBase;
using Entities.Enumeration;
using WebService.Service.Transfer;

namespace WebService.Email {

    public class CreatePurchaseRecordTemplate : BaseTemplate {

        public string OperatorId { get; set; }
        public CreatePurchaseRecordRequest Request { get; set; }
        
        public static SimpleTemplate CreatePurchaseRecordHtmlTemplate =
            SimpleTemplate.Load(Assemblys.GetString(typeof(CreateOrderRequestTemplate), 
            "Email.Resource.CreatePurchaseRecord.html")
        );

        public override string GenerateBody() {

            var product         = Services.Locate<IProductRepository>().GetProduct(Request.ProductId);
            var posterAccount   = Services.Locate<IUserRepository>().Get(Request.PosterId);
            var operatorAccount = Services.Locate<IUserRepository>().Get(OperatorId);

            var parameters = new TemplateParameters();
            parameters.Put("date",         DateTimeFormatters.YYYY_MM_DD.Format(Request.DatePurchased, DateTime.Now));
            parameters.Put("productName",  product.CName);
            parameters.Put("price",        "$"+ Request.Cost + " " + Request.CostCurrency);
            parameters.Put("count",        Request.Count);
            parameters.Put("operator",     operatorAccount.FullName);
            parameters.Put("location",     StockLocation.GetLocation(Request.Location).CName);
            parameters.Put("postage",      "$" + Request.Postage + " " + Request.PostageCurrency);
            parameters.Put("poster",       posterAccount.FullName);
            parameters.Put("currencyRate", Request.CurrencyRate);
            
            return CreatePurchaseRecordHtmlTemplate.Generate(parameters);
        }

        public override string GetSubject() {
            return "新购买记录 - " + DateTime.Today.ToLongDateString();
        }

    }
}